package service.impl;

import org.springframework.stereotype.Service;
import pojo.User;
import service.UserService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service("userServiceCH")
public class UserServiceImpl implements UserService {
	
	private Map<String,User> users;
	
	public UserServiceImpl() {
		users = new ConcurrentHashMap<>();
	}

	@Override
	public User add(User user) {
		synchronized (users) {
			users.put(user.getId(),user);
		}
		return user;
	}

	@Override
	public List<User> getAllUser() {
		List<User> userList = new ArrayList<>();
		for (String key:users.keySet()){
			userList.add(users.get(key));
		}
		return userList;
	}

	@Override
	public User delete(String id) {
		User user = null;
		if (users.containsKey(id)){
			user = users.get(id);
			users.remove(id);
		}
		return user;
	}

	@Override
	public User update(User user) {
		users.put(user.getId(),user);
		return user;
	}


}
